package com.hjh.thymeleafdemo1.Service;

import com.hjh.thymeleafdemo1.entity.Provider;
import com.hjh.thymeleafdemo1.repository.ProviderMapper;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
//配置缓存
@CacheConfig(cacheNames = "ProviderService")
public class ProviderServiceImpl implements ProviderService {

    @Resource
    private ProviderMapper providerMapper;

    @Transactional
    @Override
    public boolean insertProvider(Provider provider) {
        boolean flag = false;
        if(providerMapper.insert(provider) > 0){
            flag = true;
        }
        return flag;
    }

    @Transactional
    @Override
    public boolean deleteProvider(Long id) {
        boolean flag = false;
        if(providerMapper.deleteById(id) > 0){
            flag = true;
        }
        return flag;
    }

    @Transactional
    @Override
    public int updateProvider(Provider provider) {
        return providerMapper.updateById(provider);
    }

    @Override
    public Provider getProviderById(Long id) {
        return providerMapper.selectById(id);
    }

    @Override
    @Cacheable(value = "users")
    public List<Provider> findAllProviders() {
        return providerMapper.selectList(null);
    }
}
